package alogrithm;

import java.util.List;

/**
 * @author lbw
 * @version 1.0
 * @date 2021/10/16 16:05
 */


public class EntryNodeOfLoop {
    class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode entryNodeOfLoop(ListNode pHead) {
        ListNode fast = pHead;
        ListNode slow = pHead;

        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast.val == slow.val) {
                while (pHead != slow) {
                    slow = slow.next;
                    pHead = pHead.next;
                }
                return slow;
            }
        }
        return null;
    }
}
